Skip to content

Method: addProperties(Map, URI)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.oom;
16:
17: import cz.cvut.kbss.jopa.exceptions.OWLEntityExistsException;
18: import cz.cvut.kbss.jopa.exceptions.StorageAccessException;
19: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
20: import cz.cvut.kbss.ontodriver.Connection;
21: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
22: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
23: import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
24: import cz.cvut.kbss.ontodriver.exception.OWLIndividualExistsException;
25: import cz.cvut.kbss.ontodriver.model.Assertion;
26: import cz.cvut.kbss.ontodriver.model.NamedResource;
27: import cz.cvut.kbss.ontodriver.model.Value;
28:
29: import java.net.URI;
30: import java.util.*;
31:
32: /**
33: * Gathers axiom values in descriptor for persist or update.
34: *
35: * @author ledvima1
36: */
37: class AxiomValueGatherer {
38:
39: private final AxiomValueDescriptor axiomDescriptor;
40: private final Collection<SimpleListValueDescriptor> simpleListDescriptors = new ArrayList<>();
41: private final Collection<ReferencedListValueDescriptor> referencedListDescriptors = new ArrayList<>();
42: private Set<URI> typesToAdd;
43: private Set<URI> typesToRemove;
44: private URI typesContext;
45: private Map<Assertion, Set<Value<?>>> propertiesToAdd;
46: private Map<Assertion, Set<Value<?>>> propertiesToRemove;
47: private URI propertiesContext;
48:
49: AxiomValueGatherer(NamedResource subject, URI subjectContext) {
50: this.axiomDescriptor = new AxiomValueDescriptor(subject);
51: axiomDescriptor.setSubjectContext(subjectContext);
52: }
53:
54: void addValue(Assertion assertion, Value<?> value, URI context) {
55: addValues(assertion, Collections.<Value<?>>singleton(value), context);
56: }
57:
58: void addValues(Assertion assertion, Collection<Value<?>> values, URI context) {
59: axiomDescriptor.addAssertion(assertion);
60: for (Value<?> v : values) {
61: axiomDescriptor.addAssertionValue(assertion, v);
62: }
63: if (context != null) {
64: axiomDescriptor.setAssertionContext(assertion, context);
65: }
66: }
67:
68: void addSimpleListValues(SimpleListValueDescriptor listDescriptor) {
69: simpleListDescriptors.add(listDescriptor);
70: }
71:
72: void addReferencedListValues(ReferencedListValueDescriptor listDescriptor) {
73: referencedListDescriptors.add(listDescriptor);
74: }
75:
76: void addTypes(Set<URI> types, URI context) {
77: if (typesToAdd == null) {
78: this.typesToAdd = new HashSet<>();
79: }
80: appendTypes(typesToAdd, types, context);
81: }
82:
83: private void appendTypes(Set<URI> target, Set<URI> types, URI context) {
84: target.addAll(types);
85: this.typesContext = context;
86: }
87:
88: void removeTypes(Set<URI> types, URI context) {
89: if (typesToRemove == null) {
90: this.typesToRemove = new HashSet<>();
91: }
92: appendTypes(typesToRemove, types, context);
93: }
94:
95: void addProperties(Map<Assertion, Set<Value<?>>> properties, URI context) {
96:• if (propertiesToAdd == null) {
97: this.propertiesToAdd = new HashMap<>();
98: }
99: appendProperties(propertiesToAdd, properties, context);
100: }
101:
102: private void appendProperties(Map<Assertion, Set<Value<?>>> target, Map<Assertion, Set<Value<?>>> properties,
103: URI context) {
104: for (Map.Entry<Assertion, Set<Value<?>>> e : properties.entrySet()) {
105: if (!target.containsKey(e.getKey())) {
106: target.put(e.getKey(), e.getValue());
107: } else {
108: target.get(e.getKey()).addAll(e.getValue());
109: }
110: }
111: this.propertiesContext = context;
112: }
113:
114: void removeProperties(Map<Assertion, Set<Value<?>>> properties, URI context) {
115: if (propertiesToRemove == null) {
116: this.propertiesToRemove = new HashMap<>();
117: }
118: appendProperties(propertiesToRemove, properties, context);
119: }
120:
121: void persist(Connection connection) {
122: try {
123: connection.persist(axiomDescriptor);
124: if (typesToAdd != null) {
125: connection.types().addTypes(axiomDescriptor.getSubject(), typesContext, typesToAdd);
126: }
127: if (propertiesToAdd != null) {
128: connection.properties().addProperties(axiomDescriptor.getSubject(), propertiesContext, propertiesToAdd);
129: }
130: for (SimpleListValueDescriptor d : simpleListDescriptors) {
131: connection.lists().persistSimpleList(d);
132: }
133: for (ReferencedListValueDescriptor d : referencedListDescriptors) {
134: connection.lists().persistReferencedList(d);
135: }
136: } catch (OntoDriverException e) {
137: handlePersistException(e);
138: }
139: }
140:
141: private void handlePersistException(OntoDriverException e) {
142: if (e.getCause() instanceof OWLIndividualExistsException) {
143: throw new OWLEntityExistsException(
144: "Individual " + axiomDescriptor.getSubject() + " already exists in the ontology.", e.getCause());
145: }
146: throw new StorageAccessException(e);
147: }
148:
149: void update(Connection connection) {
150: try {
151: connection.update(axiomDescriptor);
152: if (typesToAdd != null) {
153: connection.types().addTypes(axiomDescriptor.getSubject(), typesContext, typesToAdd);
154: }
155: if (typesToRemove != null) {
156: connection.types().removeTypes(axiomDescriptor.getSubject(), typesContext, typesToRemove);
157: }
158: if (propertiesToAdd != null) {
159: connection.properties().addProperties(axiomDescriptor.getSubject(), propertiesContext, propertiesToAdd);
160: }
161: if (propertiesToRemove != null) {
162: connection.properties()
163: .removeProperties(axiomDescriptor.getSubject(), propertiesContext, propertiesToRemove);
164: }
165: for (SimpleListValueDescriptor d : simpleListDescriptors) {
166: connection.lists().updateSimpleList(d);
167: }
168: for (ReferencedListValueDescriptor d : referencedListDescriptors) {
169: connection.lists().updateReferencedList(d);
170: }
171: } catch (OntoDriverException e) {
172: throw new StorageAccessException(e);
173: }
174: }
175: }